var json = { "compressionlevel":-1,
"height":10,
"infinite":false,
"layers":[
       {
        "data":[100, 977, 943, 978, 737, 100, 100, 100, 24, 478, 476, 477, 57, 198, 24, 100, 974, 975, 976, 677, 100, 100, 100, 784, 22, 510, 511, 574, 55, 58, 100, 1006, 1007, 1008, 100, 737, 100, 933, 100, 100, 24, 479, 476, 511, 510, 100, 100, 231, 200, 100, 769, 678, 75, 76, 100, 678, 24, 477, 478, 23, 644, 100, 100, 239, 140, 100, 141, 73, 74, 676, 805, 100, 26, 27, 100, 676, 721, 71, 72, 100, 100, 143, 39, 39, 136, 100, 721, 100, 100, 199, 642, 45, 7, 7, 110, 783, 173, 167, 39, 7, 138, 100, 100, 805, 360, 674, 100, 143, 104, 100, 592, 652, 933, 103, 104, 174, 199, 264, 268, 100, 141, 135, 144, 100, 592, 524, 356, 558, 594, 100, 199, 402, 434, 202, 263, 7, 7, 168, 494, 525, 452, 521, 426, 719, 712, 297, 434, 402, 434, 402],
        "height":10,
        "id":1,
        "name":"layer01",
        "opacity":1,
        "type":"tilelayer",
        "visible":true,
        "width":15,
        "x":0,
        "y":0
       }],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.4.1",
"tileheight":16,
"tilesets":[
       {
        "columns":32,
        "firstgid":1,
        "image":"0819E5AC_0819B198_08188888_00000000.png",
        "imageheight":512,
        "imagewidth":512,
        "margin":0,
        "name":"fetile",
        "spacing":0,
        "tilecount":1024,
        "tileheight":16,
        "tilewidth":16
       }],
"tilewidth":16,
"type":"map",
"version":1.4,
"width":15
}



//创建一个fs文件
var fs = require("fs");

//读取文件
var txt = fs.readFileSync(process.argv[2]);
json = JSON.parse(txt.toString());

var set_idx = -1;
process.argv.forEach((v,k)=>{
    if(v=='-i'){
        set_idx = k+1;
    }
});

if(set_idx>0){
    set_idx =process.argv[set_idx];
}
else{
    set_idx = undefined;
}

set_idx = set_idx | 0;

//创建一个c文件
//fs.createWriteStream("asm.c");
//因为是lzss数据，所以生成一个头
var code = `
(${json.layers[0].width})|(${json.layers[0].height}<<0x8),
`;
var size = 6;  //需要加2字节
var data = [json.layers[0].width|(json.layers[0].height<<8)];
var buf16 = new Uint16Array(json.layers[0].data.length);
json.layers[0].data.forEach((v,k)=>{
    v -= 1; //需要减一，然后乘以4
    v *= 4;
    code += `0x${v.toString(16)},`;
    data.push(v);
    buf16[k] = v;
    if((k&0xf)==0){
        code += "\n";
    }
    size += 2;
});

//数据大小不能超过 41*38

//"0819E5AC_0819B198_08188888_00000000"
var name = json.tilesets[0].image;
while(1){
    //去除路径
    var idex = name.indexOf("/");
    //console.log(idex);
    if(idex>=0){
        name = name.substr(idex+1);
    }
    else{
        break;
    }
}
var list = name.match(/[0-9a-fA-F%]+/g);


code = `
//标志和大小
0|(${size&0xff}<<8),
${size}>>8,
//地图数据开始
` + code;

var idx = set_idx;




require("./lszz");
// var fs = require("fs");

var buf = lzss_encode(data);
var lzss_code = '';
buf.forEach((v,k)=>{
    if((k&0xf)==0){
        lzss_code += '\n';
    }
    lzss_code += (`0x${v.toString(16)},`);
});


code = `
[${idx}].mapData = (void*)(const u8[]){
    ${lzss_code}
},

[${idx}].tile1 = (void*)0x${list[2]},
[${idx}].tile2 = (void*)0x${list[3]},
[${idx}].pal = (void*)0x${list[0]},
[${idx}].tileSet = (void*)0x${list[1]},
`;

console.log(code);

// fs.writeFile("test.bin",buf,"binary",(err)=>{
//     console.log(err);
// });

// fs.writeFile("testsrc.bin",buf16,"binary",(err)=>console.log(err));

